<?php


namespace app\api\controller;

use app\common\controller\Api;

class Member extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 提交党员信息
     */
    public function submit()
    {
        $school = [];
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $date = array(
                'user_id' => $this->auth->id,
                'type' => $params['role'],
                'image' => $params['pics'],
                'number' => $params['number'],
                'thee_department_id' => $params['department_id'],
                'position_id' => $params['position_id'],
                'zjoining_time' => $params['zjoining_stamp'],
                'party_standing' => $params['party_standing'],
                'actions' => $params['actions'],
                'party_application_book' => $params['party_application_book'],
                'party_application_time' => $params['party_application_stamp'],
                'party_active_time' => $params['party_active_stamp'],
                'study_political_theory' => $params['study_political_theory'],
                'party_volunteer_book' => $params['party_volunteer_book'],
                'party_volunteer_time' => $params['party_volunteer_stamp'],
                'party_develop_time' => $params['party_develop_stamp'],
                'party_class_training' => $params['party_class_training'],
                'party_member_time' => $params['party_member_stamp'],
                'preparatory_expiration_time' => $params['preparatory_expiration_stamp'],
                'preparatory_class_training' => $params['preparatory_class_training'],
                'create_time' => time(),
                'status' => 0,
                'major' => $params['major'],
                'work_unit' => $params['unit'],
                'is_officer' => $params['is_officer'],
                'is_sdy' => $params['is_sdy'],
                'is_sdz' => $params['is_sdz'],
                'job' => $params['job'],
            );
            $school = array(
                'major' => $params['major'],
                'school' => $params['school'],
                'college' => $params['college'],
                'system' => $params['system'],
                'class' => $params['class'],
                'unit' => $params['unit'],
                'work_number' => $params['work_number'],
            );
            $user = array(
                'is_student' => $params['is_student'] == '是' ? 1 : 0,
                'work_number' => $params['work_number'],
            );
            $dang = model('app\admin\model\user\party\Member')->where('user_id', $this->auth->id)->find();
            if ($dang) {
                $record = model('app\admin\model\user\party\Member')->where('user_id', $this->auth->id)->update($date);
            } else {
                $record = model('app\admin\model\user\party\Member')->insert($date);
            }
            model('app\admin\model\User')->where('id', $this->auth->id)->update($user);
            model('app\admin\model\user\Userinfo')->where('user_id', $this->auth->id)->update($school);
            if ($record) {
                $this->success('提交成功');
            }
            $this->error('提交失败');
        }
        $this->error('非法请求');
    }

    /**
     * 获取党员信息
     */
    public function danginfo()
    {
        $dang = model('app\admin\model\user\party\Member')
            ->with(['user', 'userinfo'])
            ->where(model('app\admin\model\user\party\Member')->getTable() . '.user_id', $this->auth->id)
            ->find();
        $dang["school"] = model('app\admin\model\department\School')->where('id', $dang['userinfo']['school'])->find();
        $dang["college"] = model('app\admin\model\department\School')->where('id', $dang['userinfo']['college'])->find();
        $dang["system"] = model('app\admin\model\department\School')->where('id', $dang['userinfo']['system'])->find();
        $this->success('获取成功', $dang);
    }

    /**
     * 支部排名
     */
    public function branchRanking()
    {
        $params = $this->request->get();
        $branch = model('app\admin\model\department\Department')
            ->where('group_id', $params['group_id'])
            ->field('*,(SELECT COUNT(*) FROM ' . model('app\admin\model\user\party\Member')->getTable() . ' WHERE thee_department_id = ' .  model('app\admin\model\department\Department')->getTable() . '.id) as user_num')
            ->order('user_num', 'desc') // 使用 order
            ->paginate();

        $this->success('获取成功', $branch);
    }

    /**
     * 获取我的支部成员
     */
    public function dangList()
    {
        $params = $this->request->get();
        $danglist = model('app\admin\model\user\party\Member')
            ->where('thee_department_id', $params['department_id'])
            ->select();
        foreach ($danglist as $key => $value) {
            $value['user'] = model('app\common\model\User')->where('id', $value['user_id'])->find();
            $value['position'] = model('app\admin\model\department\Position')->where('id', $value['position_id'])->find();
        }
        $this->success('获取成功', $danglist);
    }
}
